using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
using YooAsset;

namespace Battle.Graphics
{
    public abstract class ActionNode : IStateNode
    {
        private ActorMachine _machine;

        public ActionNode() { }

        Animator _animator;
        ActionBaseNode actionNode;

        public void OnCreate(ActorMachine machine)
        {
            _machine = machine;
            string actionName = (string)machine.GetBlackboardValue(GetType().FullName);
            _animator = (Animator)machine.GetBlackboardValue("Animator");
            actionNode = new ActionBaseNode(_animator, actionName);
            actionNode.OnCreate();
        }

        public void OnEnter()
        {
            actionNode.Start();
        }

        public void OnExit()
        {
            actionNode.Stop();
        }

        public void OnUpdate()
        {

        }

        public void OnDestroy()
        {
            actionNode.OnDestroy();
            _animator = null;
        }
    }

    public class IdleNode : ActionNode
    {

    }

    public class MoveNode : ActionNode
    {

    }

    public class ActorPlayer : IActorGraphic
    {
        public int Id { get; set; }

        public AssetHandle AssetHandle { get; private set; }
        public GameObject GameObject { get; private set; }

        Animator _animator;
        ActorMachine _machine;

        public void OnCreate(GameObject gameObject, AssetHandle handle)
        {
            GameObject = gameObject;
            AssetHandle = handle;

            _animator = gameObject.GetComponent<Animator>();

            _machine = new ActorMachine(this);
            _machine.SetBlackboardValue("Animator", _animator);

            _machine.SetBlackboardValue(typeof(IdleNode).FullName, "zhanli");
            _machine.AddNode<IdleNode>();

            _machine.SetBlackboardValue(typeof(MoveNode).FullName, "qianjin");
            _machine.AddNode<MoveNode>();

            _machine.Run<IdleNode>();
        }

        public void OnDestroy()
        {
            _machine.Destroy();

            if (GameObject)
                Object.Destroy(GameObject);
            GameObject = null;
            if (null != AssetHandle)
                AssetHandle.Release();
            AssetHandle = null;
        }

        public void Update()
        {
            if(PlayerInput.Instance.commands.Count > 0)
            {
                var command = PlayerInput.Instance.commands[0];
                switch(command.commandType)
                {
                    case InputCommandType.Idle:
                        {
                            _machine.ChangeState<IdleNode>();
                            break;
                        }
                    case InputCommandType.MoveForward:
                        {
                            _machine.ChangeState<MoveNode>();
                            break;
                        }
                    case InputCommandType.MoveBack:
                        {
                            _machine.ChangeState<MoveNode>();
                            break;
                        }
                }
            }
        }
    }
}
